﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "AddPlatformPages.h"
#include "IApplication.h"
#include "IUiDocumentViewManager.h"
#include "IMainWindow.h"
#include "GbmpUiPlatformCommandIds.h"
#include "GcmpCommandNames.h"
#include "GcmpUiDebugCommandNames.h"
#include "IPublishConfig.h"
#include "IRibbon.h"
#include "IRibbonPage.h"
#include "IRibbonGroup.h"
#include "ICommandButtonDefinition.h"
#include "IRibbonSystemButton.h"
#include "IRibbonSystemPopupBar.h"
#include "IPageHandler.h"
#include "IRibbonPageSystemPopup.h"
#include "ICommandMenuItemDefinition.h"
#include "ICommandButtonMenuDefinition.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

class CommonPageHandler : public IPageHandler
{
public:
    CommonPageHandler() {}
    virtual ~CommonPageHandler() {}
public:
  
    virtual bool IsVisible() const override
    {
        return IUiDocumentViewManager::Get()->GetAllUiDocuments().size() > 0 ? false : true;
    }
    virtual bool IsEnabled() const override
    {
        return true;
    }
};

void AddPlatformPages::BuildSystemButton()
{
    IMainWindow* pMainwindow = IMainWindow::GetMainWindow();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMainwindow, L"pMainwindow为空",L"GDMPLab",L"2024-03-30");

    IRibbon* pRibbon = pMainwindow->GetRibbon();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pRibbon, L"pRibbon为空",L"GDMPLab",L"2024-03-30");

    IRibbonSystemButton* pSystemButton = pRibbon->GetSystemButton();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pSystemButton, L"pSystemButton为空",L"GDMPLab",L"2024-03-30");

    pSystemButton->SetTooltip(L"GbmpSysButton46");
    pSystemButton->SetIcon(L":/images/GbmpSysButton46.png");
    pSystemButton->SetText(L"GBMP");
    IRibbonSystemPopupBar* pRibbonSystemPopupBar = pSystemButton->GetRibbonSystemPopupBar();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pRibbonSystemPopupBar, L"pRibbonSystemPopupBar为空",L"GDMPLab",L"2024-03-30");

    pRibbonSystemPopupBar->AddItem(ID_CMD_NEW_FILE, GBMP_TR(L"新建"), L":/images/新建.png", L"Ctrl+N");
    pRibbonSystemPopupBar->AddItem(ID_CMD_OPEN_FILE, GBMP_TR(L"打开"), L":/images/打开.png", L"Ctrl+O");
    pRibbonSystemPopupBar->AddItem(ID_CMD_SAVE_FILE, GBMP_TR(L"保存"), L":/images/保存.png", L"Ctrl+S");
    pRibbonSystemPopupBar->AddItem(ID_CMD_SAVE_FILE_AS, GBMP_TR(L"另存为"), L":/images/另存为.png", L"");
    pRibbonSystemPopupBar->AddItem(ID_CMD_CLOSE_FILE, GBMP_TR(L"关闭"), L":/images/关闭.png", L"");
    pRibbonSystemPopupBar->AddSeparator();
    IRibbonPageSystemPopup*  pPageSystemPopup = pRibbonSystemPopupBar->AddPageSystemPopup(GBMP_TR(L"文件操作系列"), GBMP_TR(L"文件"), L":/images/新建.png");
    if (pPageSystemPopup)
    {
        pPageSystemPopup->AddItem(ID_CMD_NEW_FILE, GBMP_TR(L"新建"), L"新建项目或族文档", L":/images/新建.png");
        pPageSystemPopup->AddSeparator();
        pPageSystemPopup->AddItem(ID_CMD_OPEN_FILE, GBMP_TR(L"打开"), L"打开.gac文档", L":/images/打开.png");
        pPageSystemPopup->AddItem(ID_CMD_CLOSE_FILE, GBMP_TR(L"关闭"), L"关闭文档", L":/images/关闭.png");

        pPageSystemPopup->SetMinimumSize(200, 150);
    }
    IRibbonPageSystemPopup* pPageSystemPopupHelp = pRibbonSystemPopupBar->AddPageSystemPopup(GBMP_TR(L"帮助"), GBMP_TR(L"帮助"), L":/images/Help.png");
    if (pPageSystemPopupHelp)
    {
       pPageSystemPopupHelp->AddItem(ID_CMD_ABOUTQT, GBMP_TR(L"关于Qt"), L"关于Qt授权信息", L":/images/About.png");

       pPageSystemPopupHelp->SetMinimumSize(200, 150);
    }
}

void AddPlatformPages::AddPageCommon()
{
    IMainWindow* pMainwindow = IMainWindow::GetMainWindow();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMainwindow, L"pMainwindow为空",L"GDMPLab",L"2024-03-30");

    IRibbon* pRibbon = pMainwindow->GetRibbon();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pRibbon, L"pRibbon为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IRibbonPage> opPage = IRibbonPage::Create(ID_PAGE_COMMON, GBMP_TR(L"文件"));
    DBG_WARN_AND_RETURN_VOID_UNLESS(opPage, L"opPage为空",L"GDMPLab",L"2024-03-30");
    opPage->SetPageHandler(TransferOwnership(NEW_AS_OWNER_PTR(CommonPageHandler)));
    OwnerPtr<IRibbonGroup> opFileGroup = IRibbonGroup::Create(L"gmFileGroup", GBMP_TR(L"文件"));
    DBG_WARN_AND_RETURN_VOID_UNLESS(opFileGroup, L"opFileGroup为空",L"GDMPLab",L"2024-03-30");
    {
        OwnerPtr<ICommandButtonDefinition> opButtonDef = ICommandButtonDefinition::Create(ID_CMD_NEW_FILE, GBMP_TR(L"新建"), GBMP_TR(L"新建"), ID_CMD_NEW_FILE, L":/images/新建.png");
        opFileGroup->AddButton(TransferOwnership(opButtonDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opButtonDef = ICommandButtonDefinition::Create(ID_CMD_OPEN_FILE, GBMP_TR(L"打开"), GBMP_TR(L"打开"), ID_CMD_OPEN_FILE, L":/images/打开.png");
        opFileGroup->AddButton(TransferOwnership(opButtonDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opButtonDef = ICommandButtonDefinition::Create(ID_CMD_SAVE_FILE, GBMP_TR(L"保存"), GBMP_TR(L"保存"), ID_CMD_SAVE_FILE, L":/images/保存.png");
        opFileGroup->AddButton(TransferOwnership(opButtonDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opButtonDef = ICommandButtonDefinition::Create(ID_CMD_SAVE_FILE_AS, GBMP_TR(L"另存为"), GBMP_TR(L"另存为"), ID_CMD_SAVE_FILE_AS, L":/images/另存为.png");
        opFileGroup->AddButton(TransferOwnership(opButtonDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opButtonDef = ICommandButtonDefinition::Create(ID_CMD_CLOSE_FILE, GBMP_TR(L"关闭"), GBMP_TR(L"关闭"), ID_CMD_CLOSE_FILE, L":/images/关闭.png");
        opFileGroup->AddButton(TransferOwnership(opButtonDef));
    }
    opPage->AddGroup(TransferOwnership(opFileGroup));
    pRibbon->AddPage(TransferOwnership(opPage));
}
